import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import 'mainContract.dart';
import 'mainPresenterImpl.dart';

class MainPage extends StatefulWidget {
  final int _type;

  MainPage(this._type);

  @override
  State<StatefulWidget> createState() {
    MainPageState view = new MainPageState(_type);
    MainPresenter mainPresenter = new MainPresenterImpl(view);
    mainPresenter.init();
    return view;
  }
}

class MainPageState extends State<MainPage> implements MainView {
  MainPresenter _presenter;
  List _list = List.empty();

  final int _type;

  MainPageState(this._type);

  @override
  void initState() {
    super.initState();
    _presenter.loadFirst(_type);
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(                                                                                                                                        
      child: new StaggeredGridView.countBuilder(
        crossAxisCount: 2,
        itemCount: _list.length,
        itemBuilder: (BuildContext context, int index) => new Container(
          child: new Stack(
            children: [
              FadeInImage.memoryNetwork(image: _list[index]),
              Container(
                child: Align(
                  alignment: Alignment.bottomCenter,
                  child: Stack(
                    children: [
                      Text(_list[index]),
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
        staggeredTileBuilder: (int index) =>
            new StaggeredTile.count(2, index.isEven ? 2 : 1),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
      ),
      onRefresh: () => _handleRefresh(),
    );
  }

  @override
  void loadFirstError(int code) {}

  @override
  void loadFirstSuccess(data) {
    setState(() {
      _list.addAll(data);
    });
  }

  @override
  void loadMoreError(int code) {}

  @override
  void loadMoreSUccess(data) {}

  @override
  setPresenter(presenter) {
    _presenter = presenter;
  }

  _handleRefresh() {}
}
